# -*- coding: utf-8 -*-
__author__ = 'pzx'


from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow,QDockWidget
from PyQt5.QtQuickWidgets import QQuickWidget
from PyQt5.QtGui import QIcon,QGuiApplication,QWindow
from PyQt5.QtQuick import QQuickView
from Slider import Ui_Form
from PyQt5.QtCore import QUrl,QObject,QRect,Qt,pyqtSignal
from Meter import qrc_qml           #导入qrc资源文件
import sys


class MyDockWidget(QDockWidget):
    signalDockWidgetResize = pyqtSignal()
    def resizeEvent(self,event):
        self.signalDockWidgetResize.emit()


class MyWindow(QMainWindow,Ui_Form):
    def __init__(self,parent=None):
        super(MyWindow,self).__init__(parent)
        self.setupUi(self)
        self.setWindowTitle('仪表')                           #设置窗口标题
        self.meterView=QQuickView()                           #创建一个QQuickView实例，用来加载qml文件
        self.meterView.setSource(QUrl("qrc:/Meter.qml"))      #加载qml文件
        self.meterViewObject = self.meterView.rootObject()    # 创建QML与Python交互的接口，主要是与暴露出来的property类型交互，这个一定要在
                                                              # setSource函数后面，不然会出现不可描述的错误

        self.meterViewObject.setProperty("width", 500)        # 设置QML中预定义的变量
        self.meterViewObject.setProperty("height", 400)
        self.meterViewObject.setProperty("meter_value", 0)
        self.meterViewObject.setProperty("meterMaxValue", 500)
        self.meterViewObject.setProperty("meterMinValue", -500)
        self.meterViewObject.setProperty("underMeterText", 'Km/h')

        # 创建一个窗口容器，用来显示QQuickView类，因为QQuickView类不能直接显示在MainWindow上，或者说其父类不能为QMainWindow，只能为QWindow
        # 而通过窗口容器就作为中介实现QMainWindow的二级子类为QQuickView
        self.meterWindow=self.createWindowContainer(self.meterView,parent=self)
        # self.meterWindow.setGeometry(QRect(0,0,200,200)) #设置窗口容器的起点坐标和大小

        #DockWiget
        self.meterDockWindow=MyDockWidget(self.tr("仪表"))
        self.meterDockWindow.setFeatures(QDockWidget.AllDockWidgetFeatures)
        self.meterDockWindow.setWidget(self.meterWindow)
        self.meterDockWindow.setMinimumSize(640,200)                         #设置最小大小
        self.meterDockWindow.setMaximumSize(640,200)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.meterDockWindow)
        self.meterDockWindow.signalDockWidgetResize.connect(self.slotDockWidgetResize)


    def mySliderValueChanged(self,value):
        self.meterViewObject.setProperty("meter_value", value)

    def myLineEdit(self,valueString):
        self.meterViewObject.setProperty("meter_value", valueString)

    def slotDockWidgetResize(self):
        self.meterViewObject.setProperty("width", self.meterDockWindow.width())  # 设置QML中预定义的变量
        self.meterViewObject.setProperty("height", self.meterDockWindow.height())




if __name__ == "__main__":             #主函数
    app = QApplication(sys.argv)       #创建一个应用程序
    w=MyWindow()
    w.show()
    sys.exit(app.exec())               #退出程序


